package cn.tedu.jxc.controller;

import cn.tedu.jxc.pojo.dto.CustomerAddNewDTO;
import cn.tedu.jxc.pojo.entity.Customer;
import cn.tedu.jxc.pojo.vo.CustomerListVO;
import cn.tedu.jxc.pojo.vo.CustomerVO;
import cn.tedu.jxc.service.CustomerService;
import cn.tedu.jxc.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 客户类
 */
@Slf4j
@Api(tags = "03. 客户类")
@Validated
@RestController
@RequestMapping("/customers")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    @ApiOperation(value = "添加客户")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> insert(CustomerAddNewDTO customerAddNewDTO) {
        log.debug("开始处理[添加客户]的请求,参数:{}", customerAddNewDTO);
        customerService.insert(customerAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation(value = "删除客户")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> deleteById(@PathVariable Long id) {
        log.debug("开始处理[删除客户]的请求,参数:{}", id);
        customerService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation(value = "修改客户")
    @ApiOperationSupport(order = 300)
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> updateById(@PathVariable Long id,CustomerAddNewDTO customerAddNewDTO) {
        log.debug("开始处理【修改客户详情】的请求，参数ID：{}, 新数据：{}", id, customerAddNewDTO);
        customerService.updateById(id,customerAddNewDTO);
        return JsonResult.ok();
    }
    @ApiOperation(value = "根据客户id查询客户详情")
    @ApiOperationSupport(order = 400)
    @GetMapping("/{id:[0-9]+}/getStandardById")
    public JsonResult<CustomerVO> getStandardById(@PathVariable Long id) {
        log.debug("开始处理[查询客户列表]的请求,参数:{}", id);
        CustomerVO standardByName = customerService.getStandardById(id);
        return JsonResult.ok(standardByName);
    }
    @ApiOperation(value = "根据客户名称查询客户详情")
    @ApiOperationSupport(order = 4100)
    @GetMapping("/getStandardCustomer")
    public JsonResult<List<CustomerListVO>> getStandardByName (String name){
        log.debug("开始处理[查询客户列表]的请求,参数:{}", name);
        List<CustomerListVO> standardByName = customerService.getStandardByName(name);
        return JsonResult.ok(standardByName);
    }
    @ApiOperation(value = "查询客户列表")
    @ApiOperationSupport(order = 4200)
    @GetMapping("/list")
    public JsonResult<List<CustomerListVO>> list() {
        log.debug("开始处理[查询客户列表]的请求,无参数");
        List<CustomerListVO> list = customerService.list();
        return JsonResult.ok(list);
    }
}
